#include "ui_avatar_widget.h"

UiAvatarWidget::UiAvatarWidget(QWidget *parent)
    : QWidget{parent},avatar_type_(0)
{
    avatar_label_ = new QLabel(this);
    avatar_label_->setFixedSize(60,60);

    current_avatar_ = nullptr;

    v_box_layout_ = new QVBoxLayout(this);
    v_box_layout_->addStretch();
    v_box_layout_->addWidget(avatar_label_);
    v_box_layout_->addStretch();
}

UiAvatarWidget::~UiAvatarWidget()
{
    if(current_avatar_ != nullptr)
    {
        delete current_avatar_;
        current_avatar_ = nullptr;
    }
}

void UiAvatarWidget::SetAvatarType(const int &type)
{
    avatar_type_ = type;

    ClearAll();

    switch(avatar_type_)
    {
        case enum_avatar_type::ENUM_AVATAR_CUSTOM:
        {
            current_avatar_ = AvatarFactory::Instance()->NewAvatar(ENUM_AVATAR_CUSTOM);
        }
        break;

        case enum_avatar_type::ENUM_AVATAR_EVIL:
        {
            current_avatar_ = AvatarFactory::Instance()->NewAvatar(ENUM_AVATAR_EVIL);
        }
        break;

        case enum_avatar_type::ENUM_AVATAR_ANGEL:
        {
            current_avatar_ = AvatarFactory::Instance()->NewAvatar(ENUM_AVATAR_ANGEL);
        }
        break;

        case enum_avatar_type::ENUM_AVATAR_ACTOR:
        {
            current_avatar_ = AvatarFactory::Instance()->NewAvatar(ENUM_AVATAR_ACTOR);
        }
        break;
    }

}

void UiAvatarWidget::SetAvatarTypeCustom(QMap<QString, QString> res_map, bool is_gif)
{
    ClearAll();

    current_avatar_ = AvatarFactory::Instance()->NewAvatar(ENUM_AVATAR_CUSTOM);
    is_gif_ = is_gif;

    QMap<QString,QString>::const_iterator c_iter = res_map.cbegin();
    while(c_iter != res_map.cend())
    {
        if(is_gif_)
        {
            current_avatar_->AddMovie(c_iter.key(),c_iter.value());
        }
        else
        {
            current_avatar_->AddImage(c_iter.key(),c_iter.value());
        }
        ++c_iter;
    }
}

void UiAvatarWidget::SetMovie(const QString &avatar_status)
{
    if(is_gif_)
    {
        final_avatar_gif_.setFileName(current_avatar_->GetMoviePath(avatar_status));
        final_avatar_gif_.setScaledSize(avatar_label_->size());
        avatar_label_->setMovie(&final_avatar_gif_);
        final_avatar_gif_.start();
    }
    else
    {
        final_avatar_img_.load(current_avatar_->GetImagePath(avatar_status));
        QImage temp_img = final_avatar_img_.scaled(avatar_label_->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation);
        final_avatar_img_ = temp_img;
        avatar_label_->setPixmap(QPixmap::fromImage(final_avatar_img_));
    }
}

void UiAvatarWidget::SetGifPlayTimes(const int &play_mode)
{
    play_mode_ = play_mode;

    if(is_gif_)
    {
        if(0 == play_mode || 1 == play_mode)//只播放一次
        {
            final_avatar_gif_.disconnect();
            connect(&final_avatar_gif_,SIGNAL(frameChanged(int)),this,SLOT(slot_play_control(int)));
        }
        else if(2 == play_mode)//保持gif设定
        {
            return ;
        }
    }
}

void UiAvatarWidget::ClearFinalAvatarImage()
{
    final_avatar_img_ = QImage();
}

void UiAvatarWidget::ClearFinalAvatarMovie()
{
    final_avatar_gif_.setFileName("");
}

void UiAvatarWidget::ClearAll()
{
    ClearFinalAvatarImage();
    ClearFinalAvatarMovie();
    ClearAvatar();
}

void UiAvatarWidget::slot_play_control(int frame_number)
{
    if(is_gif_)
    {
        if(0 == play_mode_)//只播放一次
        {
            if(frame_number == final_avatar_gif_.frameCount() - 1)
            {
                final_avatar_gif_.stop();
            }
        }
        else if(1 == play_mode_)//循环播放
        {
            if(frame_number == final_avatar_gif_.frameCount())
            {
                final_avatar_gif_.jumpToFrame(0);
            }
        }
        else if(2 == play_mode_)//保持gif原始设定
        {
            return;
        }
    }
}
